package com.yh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yh.entity.UserInfo;
import com.yh.mapper.UserInfoMapper;
import com.yh.service.DatabaseLock;
import org.springframework.stereotype.Service;

/**
 * 数据库控制 乐观锁 悲观锁
 * @author yh
 */
@Service
public class DatabaseLockImpl implements DatabaseLock {

    private UserInfoMapper userInfoMapper;
    private DatabaseLockImpl(UserInfoMapper userInfoMapper){
        this.userInfoMapper = userInfoMapper;
    }

    @Override
    public void updateUser(UserInfo userInfo, Integer version) {
        Integer oldVersion = userInfo.getVersion();
        userInfo.setVersion(userInfo.getVersion() + 1);
        userInfoMapper.update(userInfo,new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getId,userInfo.getId()).eq(UserInfo::getVersion,oldVersion));
    }
}
